package org.example;

import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class FeedbackService {
    private final Map<String, Integer> conversationFeedbacks = new ConcurrentHashMap<>();
    
    /**
     * 保存会话评价
     * @param conversationId 会话ID
     * @param rating 评价等级 (1-5)
     */
    public void saveFeedback(String conversationId, int rating) {
        if (rating < 1 || rating > 5) {
            throw new IllegalArgumentException("Rating must be between 1 and 5");
        }
        conversationFeedbacks.put(conversationId, rating);
    }
    
    /**
     * 获取会话评价
     * @param conversationId 会话ID
     * @return 评价等级 (1-5), 如果没有评价返回0
     */
    public int getFeedback(String conversationId) {
        return conversationFeedbacks.getOrDefault(conversationId, 0);
    }
    
    /**
     * 获取平均评价
     * @return 所有会话的平均评价
     */
    public double getAverageRating() {
        if (conversationFeedbacks.isEmpty()) {
            return 0;
        }
        return conversationFeedbacks.values().stream()
                .mapToInt(Integer::intValue)
                .average()
                .orElse(0);
    }
}
